import request from '@/util/axios';
// import 
import config from '@/config';
import { getToken } from '@/util/auth'

import { ElMessage } from 'element-plus'


const baseUrl = config.baseUrl.dev;

request.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'

const service = request.create({
    baseURL: process.env.API_ROOT, // 设置请求的基本URL
    timeout: 5000, // 请求超时时间
    withCredentials: true, //表示跨域请求时是否需要使用凭证
})

service.interceptors.request.use(
    config => {
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
        config.headers['Authorization'] =
            'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaW5mbyI6IntcImFnZVwiOjMyLFwiYXVkaXRUaW1lXCI6XCIyMDIzLTExLTA4IDEwOjQwOjM4XCIsXCJhdXRob3JpdGllc1wiOlt7XCJpZFwiOjksXCJtZW51SWRcIjozLFwibmFtZVwiOlwi6aaW6aG15p2D6ZmQMVwiLFwidXJsXCI6XCLmnYPpmZAx6Lev5b6EXCJ9LHtcImlkXCI6MTEsXCJtZW51SWRcIjozLFwibmFtZVwiOlwi6aaW6aG15p2D6ZmQMlwiLFwidXJsXCI6XCIyMzEzMVwifSx7XCJpZFwiOjEyLFwibWVudUlkXCI6NSxcIm5hbWVcIjpcIuWtkOmhtemdouadg-mZkDFcIixcInVybFwiOlwiMTIzMVwifSx7XCJpZFwiOjEzLFwibWVudUlkXCI6NSxcIm5hbWVcIjpcIuWtkOmhtemdouadg-mZkDJcIixcInVybFwiOlwiMjMx5ZWKXCJ9LHtcImlkXCI6MTUsXCJtZW51SWRcIjo0LFwibmFtZVwiOlwi55uu5b2VMVwiLFwidXJsXCI6XCIyMzHllYpcIn0se1wiaWRcIjoxNyxcIm1lbnVJZFwiOjQsXCJuYW1lXCI6XCLnm67lvZUyXCIsXCJ1cmxcIjpcIjIxM-WVilwifV0sXCJnZW5kZXJcIjoxLFwiaWRcIjoxOSxcInBob25lXCI6XCIxMjMxXCIsXCJwbGF0Zm9ybVwiOntcImNvZGVcIjpcIldhbmdXZW5RaWFuZ1wiLFwiY3JlYXRlVGltZVwiOlwiMjAyMy0xMS0wMyAxMDo1MzozM1wiLFwiZW5hYmxlXCI6dHJ1ZSxcImlkXCI6MTUsXCJuYW1lXCI6XCLnjovmloflvLpcIixcInVwZGF0ZVRpbWVcIjpcIjIwMjMtMTEtMDYgMDk6NDA6MTBcIixcInVyaVwiOlwicXFcIn0sXCJyZWdpc3RlclRpbWVcIjpcIjIwMjMtMTEtMDggMTA6NDA6MzhcIixcInVzZXJuYW1lXCI6XCI3ODhcIn0iLCJpYXQiOjE2OTk0MTUyMjIsInN1YiI6Ijc4OCIsImV4cCI6MTY5OTQxNzAyMn0.Lfm26cRBQq5SREAivS80OfJE-RhlmwZS6mEoGFMDUd4'
        return config
    },
    error => {
        Promise.reject(error)
    }
)



service.interceptors.response.use(response => {
    if(response.data.code != 200){
        ElMessage.error(response.data.msg);
    }
  }, error => {
    // if (error.response.status == 401) redirect()
    Promise.reject(error)
  })

let Urequest = {
    post: (url, data) => {
        return new Promise((resolve, reject) => {
            service.post(baseUrl + url, data)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err.data)
                })
        });
    },
    put: (url, data) => {
        return new Promise((resolve, reject) => {
            service.put(baseUrl + url, data)
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err.data)
                })
        });
    },
    delete: (url, params) => {
        return new Promise((resolve, reject) => {
            service.delete(baseUrl + url, {
                params: params
            })
                .then(res => {
                    resolve(res);
                })
                .catch(err => {
                    reject(err.data)
                })
        });
    },
    get: (url, params) => {
        return new Promise((resolve, reject) => {
            service.get(baseUrl + url, {
                params: params
            }).then(res => {
                resolve(res);

            }).catch(err => {
                reject(err.data)
            })
        });
    }
}

export default Urequest;


